import request from '@/utils/request'

// 查询溯源码主列表
export function listMain(query) {
  return request({
    url: '/md/main/list',
    method: 'get',
    params: query
  })
}

// 查询溯源码主详细
export function getMain(id) {
  return request({
    url: '/md/main/' + id,
    method: 'get'
  })
}

// 新增溯源码主
export function addMain(data) {
  return request({
    url: '/md/main',
    method: 'post',
    data: data
  })
}

// 修改溯源码主
export function updateMain(data) {
  return request({
    url: '/md/main',
    method: 'put',
    data: data
  })
}

// 删除溯源码主
export function delMain(id) {
  return request({
    url: '/md/main/' + id,
    method: 'delete'
  })
}
// 在 @/api/mes/qr/main.js 中添加
export function generateQrCodes(batchNumber, quantity, mainId) {
  return request({
    url: '/md/main/generate',
    method: 'post',
    params: { batchNumber, quantity, mainId } // 确保带上 mainId
  });
}

// 获取子表详情
export function getQrSubDetail(code) {
  return request({
    url: '/md/main/scan/' + code,
    method: 'get'
  });
}

// export function uploadFiles(files, type) {
//   const formData = new FormData();
//   files.forEach(file => {
//     formData.append('files', file);
//   });
//   formData.append('type', type);

//   return request({
//     url: '/md/main/uploadImages',
//     method: 'post',
//     data: formData,
//     headers: {
//       'Content-Type': 'multipart/form-data'
//     }
//   });
// }
export function uploadImage(data) {
  return request({
    url: '/md/main/uploadImage',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}
// 批量下载二维码
export function downloadBatchQrCodes(data) {
  return request({
    url: '/md/main/downloadBatch',
    method: 'post',
    data: data,
    responseType: 'blob'
  })
}
// 新增异步下载相关API函数
export function downloadBatchQrCodesAsync(data) {
  return request({
    url: '/md/main/downloadBatchAsync',
    method: 'post',
    data: data
  })
}

export function getDownloadTaskStatus(taskId) {
  return request({
    url: '/md/main/downloadTaskStatus/' + taskId,
    method: 'get'
  })
}

export function cancelDownloadTask(taskId) {
  return request({
    url: '/md/main/cancelDownloadTask/' + taskId,
    method: 'post'
  })
}

export function downloadTaskFile(taskId) {
  return request({
    url: '/md/main/downloadTaskFile/' + taskId,
    method: 'get',
    responseType: 'blob'
  });
}



